﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Text;


namespace DynimacFileReader
{
    #region
    public class DynamicFileHelper
    {

        public IEnumerable<dynamic> ParseFile(string file)
        {
            var retList = new List<dynamic>();


            var fileStream = OpenFile(file);
            if (fileStream != null)
            {
                string[] headerLine = fileStream.ReadLine().Split('%').Select(s => s.Trim()).ToArray();//定义所有符号要一样
                while (fileStream.Peek() > 0)
                {
                    string[] dataLine = fileStream.ReadLine().Split('%');//定义所有符号要一样，文本也是一样
                    dynamic dyEntity = new ExpandoObject();

                    for (int i = 0; i < headerLine.Length; i++)
                    {
                        ((IDictionary<string, object>)dyEntity).Add(headerLine[i], dataLine[i]);
                    }
                    retList.Add(dyEntity);

                }

            }
            return retList;

        }
        private StreamReader OpenFile(string fileName)
        {
            if (File.Exists(fileName))
            {
                return new StreamReader(File.OpenRead(fileName));
            }
            return null;
        }

    }
    #endregion
}



